package com.ruoyi.cderp.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.cderp.domain.*;
import com.ruoyi.cderp.mapper.TblOrderPayTypeMapper;
import com.ruoyi.cderp.mapper.TblProductStockMapper;
import com.ruoyi.cderp.mapper.TblPurchaseOrderMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.mapper.TblSaleOrderMapper;
import com.ruoyi.cderp.service.ITblSaleOrderService;

/**
 * 销售订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-15
 */
@Service
public class TblSaleOrderServiceImpl implements ITblSaleOrderService
{
    // 存储当前登录用户对象
    LoginUser loginUser = null;
    @Autowired
    private TblSaleOrderMapper tblSaleOrderMapper;
    @Autowired
    private TblPurchaseOrderMapper tblPurchaseOrderMapper;
    @Autowired
    private TblOrderPayTypeMapper tblOrderPayTypeMapper;
    @Autowired
    private TblProductStockMapper tblProductStockMapper;

    /**
     * 查询销售订单
     *
     * @param id 销售订单主键
     * @return 销售订单
     */
    @Override
    public TblSaleOrder selectTblSaleOrderById(String id)
    {
        return tblSaleOrderMapper.selectTblSaleOrderById(id);
    }

    /**
     * 查询销售订单列表
     *
     * @param tblSaleOrder 销售订单
     * @return 销售订单
     */
    @Override
    public List<TblSaleOrder> selectTblSaleOrderList(TblSaleOrder tblSaleOrder)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();

        return tblSaleOrderMapper.selectTblSaleOrderList(tblSaleOrder);
    }

    /**
     * 新增销售订单
     *
     * @param tblSaleOrder 销售订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblSaleOrder(TblSaleOrder tblSaleOrder)
    {
        tblSaleOrder.setCreateTime(DateUtils.getNowDate());

        if (tblSaleOrder.getSalerId() == null) {
            tblSaleOrder.setSalerId(loginUser.getUser().getUserId().toString());
        }
        tblSaleOrder.setCreateById(loginUser.getUser().getUserId().toString());
        tblSaleOrder.setCreateBy(loginUser.getUser().getUserName());

        int rows = tblSaleOrderMapper.insertTblSaleOrder(tblSaleOrder);
        insertTblSaleOrderDetail(tblSaleOrder);
        insertTblOrderPayType(tblSaleOrder);
        return rows;
    }

    /**
     * 修改销售订单
     *
     * @param tblSaleOrder 销售订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblSaleOrder(TblSaleOrder tblSaleOrder)
    {
        if(tblSaleOrder.getStatus()==3){
            for (TblSaleOrderDetail tblSaleOrderDetail : tblSaleOrder.getTblSaleOrderDetailList()) {
                tblProductStockMapper.updateTblProductStock(tblSaleOrderDetail.getBaseDataProduct().getTblProductStock());
            }
        }
        tblSaleOrder.setUpdateTime(DateUtils.getNowDate());

        tblSaleOrder.setUpdateById(loginUser.getUser().getUserId().toString());
        tblSaleOrder.setUpdateBy(loginUser.getUser().getUserName());
        if (tblSaleOrder.getApproveBy()!=null){
            tblSaleOrder.setApproveBy((loginUser.getUser().getUserName()));
        }

        tblSaleOrderMapper.deleteTblSaleOrderDetailByOrderId(tblSaleOrder.getId());
        insertTblSaleOrderDetail(tblSaleOrder);
        tblOrderPayTypeMapper.deleteTblOrderPayTypeById(tblSaleOrder.getId());
        insertTblOrderPayType(tblSaleOrder);
        return tblSaleOrderMapper.updateTblSaleOrder(tblSaleOrder);
    }

    /**
     * 批量删除销售订单
     *
     * @param ids 需要删除的销售订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblSaleOrderByIds(String[] ids)
    {
        tblSaleOrderMapper.deleteTblSaleOrderDetailByOrderIds(ids);
        deleteTblOrderPayType(ids);
        return tblSaleOrderMapper.deleteTblSaleOrderByIds(ids);
    }

    /**
     * 删除销售订单信息
     *
     * @param id 销售订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblSaleOrderById(String id)
    {
        tblSaleOrderMapper.deleteTblSaleOrderDetailByOrderId(id);
        return tblSaleOrderMapper.deleteTblSaleOrderById(id);
    }

    @Override
    public TblSaleConfig selectTblSaleConfigInfo() {
        return tblSaleOrderMapper.selectTblSaleConfigInfo();
    }

    @Override
    public int updateTblSaleConfig(TblSaleConfig tblSaleConfig) {
        return tblSaleOrderMapper.updateTblSaleConfig(tblSaleConfig);
    }

    @Override
    public List<BaseDataCustomer> customer() {
        return tblSaleOrderMapper.customer();
    }

    @Override
    public List<BaseDataStoreCenter> center(Center center) {
        return tblSaleOrderMapper.center(center);
    }

    /**
     * 新增销售订单明细信息
     *
     * @param tblSaleOrder 销售订单对象
     */
    public void insertTblSaleOrderDetail(TblSaleOrder tblSaleOrder)
    {
        List<TblSaleOrderDetail> tblSaleOrderDetailList = tblSaleOrder.getTblSaleOrderDetailList();
        String id = tblSaleOrder.getId();
        if (StringUtils.isNotNull(tblSaleOrderDetailList))
        {
            List<TblSaleOrderDetail> list = new ArrayList<TblSaleOrderDetail>();
            for (TblSaleOrderDetail tblSaleOrderDetail : tblSaleOrderDetailList)
            {
                tblSaleOrderDetail.setOrderId(id);
                list.add(tblSaleOrderDetail);
            }
            if (list.size() > 0)
            {
                tblSaleOrderMapper.batchTblSaleOrderDetail(list);
            }
        }
    }

    /**
     * 新增销售支付记录
     *
     * @param tblSaleOrder 销售订单对象
     */
    public void insertTblOrderPayType(TblSaleOrder tblSaleOrder)
    {
        List<TblOrderPayType> tblOrderPayTypeList = tblSaleOrder.getTblOrderPayTypeList();
        String id = tblSaleOrder.getId();
        if (StringUtils.isNotNull(tblOrderPayTypeList))
        {
            List<TblOrderPayType> list = new ArrayList<TblOrderPayType>();
            for (TblOrderPayType tblOrderPayType : tblOrderPayTypeList)
            {
                tblOrderPayType.setOrderId(id);
                list.add(tblOrderPayType);
            }
            if (list.size() > 0)
            {
                for (TblOrderPayType tblOrderPayType : list) {
                    if (tblOrderPayType.getPayAmount().compareTo(BigDecimal.ZERO) > 0) {
                        tblOrderPayTypeMapper.insertTblOrderPayType(tblOrderPayType);
                    }
                }
            }
        }
    }

    // 删除销售订单支付记录
    public void deleteTblOrderPayType(String[] ids)
    {
        for (String id : ids) {
            tblOrderPayTypeMapper.deleteTblOrderPayTypeById(id);
        }
    }
}
